package frame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import thread.QQClientThread;
import util.DBUtil;

public class LoginFrame extends JFrame implements ActionListener {
	private String userName;
	private String loginName;
	private String password;
	
	private JTextField nameTf;
	private JPasswordField  psdPf;
	private JButton loginBtn;
	private JButton cancelBtn;
	private JButton signBtn; 
	public LoginFrame() {
		loginName = null;
		userName = null;
		this.setSize(400, 260);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		init();
		this.setVisible(true);
	}
	private void init(){
		this.setLayout(null);
		nameTf=new JTextField();
		psdPf=new JPasswordField();
		loginBtn=new JButton("login");
		signBtn = new JButton("sign");
		cancelBtn=new JButton("cancel");
		nameTf.setBounds(20, 20, 290, 40);
		psdPf.setBounds(20, 80, 290, 40);
		loginBtn.setBounds(20, 140, 90, 40);
		cancelBtn.setBounds(220, 140, 90, 40);
		signBtn.setBounds(120, 140, 90, 40);
		this.add(nameTf);
		this.add(psdPf);
		this.add(loginBtn);
		this.add(cancelBtn);
		this.add(signBtn);
		
		loginBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				loginName = nameTf.getText();
				password = psdPf.getText();
				psdPf.setText("");
				if(loginName.equals("") || password.equals("")){
					JOptionPane.showMessageDialog(null, "password or username should not empty...");
					return;
				}
				
				try {
					Socket socket = new Socket("127.0.0.1", 8003);
					BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
					BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
					writer.write(loginName + "\\" + password + "\n");
					writer.flush();
					
					String loginString = reader.readLine();
					if(loginString.equals("202")){
						JOptionPane.showMessageDialog(null, "log in success...");
						new QQClientThread(reader, writer, loginName, socket).start();
					}
					else if(loginString.equals("000")){
						JOptionPane.showMessageDialog(null, "user is not exit, please sign in...");
					}
					else if(loginString.equals("404")){
						JOptionPane.showMessageDialog(null, "The user is online, "
								+ "please choose another id to login");
					}
					else if(loginString.equals("303")){
						JOptionPane.showMessageDialog(null, "log in error...");
					}
					else if(loginString.equals("505")){
						JOptionPane.showMessageDialog(null, "password is wrong, "
						+ "please enter again...");
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				} 
			}
		});
		
		signBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				loginName = nameTf.getText();
				password = psdPf.getText();
				
				if(loginName.equals("") || password.equals("")){
					JOptionPane.showMessageDialog(null, "password or username should not empty...");
					return;
				}
				nameTf.setText("");
				psdPf.setText("");
				
				try {
					Socket socket = new Socket("127.0.0.1", 8009);
					BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
					BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
					writer.write(loginName + "\\" + password + "\n");
					writer.flush();
					String signString = reader.readLine();
					
					if(signString.equals("200")){
						JOptionPane.showMessageDialog(null, "sign in success...");
					}
					else if(signString.equals("000")){
						JOptionPane.showMessageDialog(null, "user is exit, please change your sign name...");
					}
					else if(signString.equals("404")){
						JOptionPane.showMessageDialog(null, "it a exception..., try it again...");
					}
					
				} catch (Exception e1) {
					e1.printStackTrace();
				} 
				
			}
		});
		
		cancelBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
	}
	public String getLogin(){
		return userName;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
	}
}